<?php

namespace App\Models;

use App\Traits\InsertOnDuplicateKey;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class ZPituitaryPatientsNotice extends Model
{

    use InsertOnDuplicateKey;

    protected $table = 'z_pituitary_patients_notice';

    protected $guarded = ['id'];

    public static function updatePatientsNotice($nreserveDate, $nremaining_days, $noverlapDays, $ntype, $nhospId, $ndeptId, $nuserId)
    {
        $sql_notice = "update z_pituitary_patients_notice set actual_reserva = '$nreserveDate',remaining_days = '$nremaining_days',overlap_days='$noverlapDays',type='$ntype' where hosp_id = '$nhospId'   and user_id  = '$nuserId' ";
        return DB::update($sql_notice);
    }


    /**
     * 提醒信息入库
     * @param array $result
     * @return array
     */
    public static function insertPatientsNoticeNew($result)
    {

        try {
            foreach ($result as $item) {
                ZPituitaryPatientsNotice::insertOnDuplicateKey($item);
            }
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
        return ['code' => 0];
    }

    /**
     *  更新倒计时时间
     * @return int
     */
    public static function updatePatientNoticeCountDown()
    {
        return self::query()
            ->where('count_down', '!=', '0001-01-01 00:00:00')
            ->where('count_down', '<=', date("Y-m-d H:i:s"))
            ->update(['count_down' => '0001-01-01 00:00:00']);
    }


    /**
     * 获取漏诊条数
     * @param $hospId
     * @param $deptId
     * @param $source
     * @return int
     */
    public static function getOverlaps($hospId, $deptId, $source)
    {
        $sqlCount = "select count(id) as cnt from z_pituitary_patients_notice where hosp_id = '$hospId' and  dept_id = $deptId and state = 0 and type = 2";
        $ret = DB::selectOne($sqlCount);
        return isset($ret->cnt) ? $ret->cnt : 0;
    }


    /**
     *  更新提醒时间
     * @param $date
     * @return bool
     */
    public static function updateDate($date)
    {
        //带预约
        $sql1 = "update z_pituitary_patients_notice
                    set remaining_days = DATEDIFF(actual_reserva, '$date'  )  , overlap_days = '-1' ,type = '1'
                     where actual_reserva !='0001-01-01' and actual_reserva >= '$date'";
        //带预约过期
        $sql2 = "update z_pituitary_patients_notice
                    set overlap_days = DATEDIFF('$date' ,actual_reserva )  , remaining_days = '-1' ,type = '2'
                     where actual_reserva !='0001-01-01' and actual_reserva < '$date'";
        //推荐日期
        $sql3 = "update z_pituitary_patients_notice
                    set remaining_days = DATEDIFF(suggest_reserva, '$date'  )  , overlap_days = '-1' ,type = '1'
                   where  actual_reserva  ='0001-01-01' and suggest_reserva >= '$date'";
        //推荐日期过期
        $sql4 = "update z_pituitary_patients_notice
                    set overlap_days = DATEDIFF( '$date' ,suggest_reserva )  , remaining_days = '-1' ,type = '2'
                   where actual_reserva  ='0001-01-01' and suggest_reserva < '$date'";

        DB::update($sql1);
        DB::update($sql2);
        DB::update($sql3);
        DB::update($sql4);

        return true;

    }

}
